<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#list entities.entity as entity>
    <#assign entityName = entity.@name>
    <#assign fileName = "${javaDir}${packageDir}/service/"+entity.@name?cap_first+"ServiceBase.java">
    <@pp.changeOutputFile name="${fileName}" />

package ${project.@rootPackage}.service;

import ${project.@rootPackage}.service.support.AbstractService;
import ${project.@rootPackage}.dao.${entityName}Dao;
import ${project.@rootPackage}.domain.${entityName};
    <#list entity["one-to-one"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>
    <#list entity["many-to-one"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>
	<#list entity["many-to-many"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>
<#include "/includes/fixImportsList.inc">
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service class for entity ${entity.@name}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class ${entityName}ServiceBase extends AbstractService<${entityName}> {

	protected static final Logger logger = Logger.getLogger("${entityName}Service");
	
    @Autowired
    protected ${entityName}Dao ${entityName?uncap_first}Dao;

    @Override
    public ${entityName}Dao getDao() {
        return ${entityName?uncap_first}Dao;
    }
    
	<#list entity["one-to-many"] as rel>
    @Autowired
    protected ${rel.@entity}Service ${rel.@entity?uncap_first}Service;
	
	</#list>
	
	<#if entity.column.@name?seq_contains("baja")>
    public List<${entityName}> findAllNotBaja() {
        return getDao().findAllNotBaja();
    }
    </#if>
	
	<#if entity["one-to-many"][0]??>
    @Transactional
    @Override
    public boolean delete(${entityName} entity) {
        <#list entity["one-to-many"] as rel>
        ${rel.@entity?uncap_first}Service.delete(${rel.@entity?uncap_first}Service.findBy${entityName}(entity));
        </#list>
        return getDao().delete(entity);
    }
	</#if>
	
	<#list entity["one-to-one"] as rel>
    public ${entityName} findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}){
        return getDao().findBy${rel.@entity}(${rel.@name?uncap_first});
    }
	
    </#list>
	<#list entity["many-to-one"] as rel>
    public List<${entityName}> findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}){
        return getDao().findBy${rel.@entity}(${rel.@name?uncap_first});
    }
	
    </#list>
	<#list entity["many-to-many"] as rel>
    public List<${entityName}> findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}){
        return getDao().findBy${rel.@entity}(${rel.@name?uncap_first});
    }
	
    </#list> 


   <#if includeLoginJsf && (entityName = config.login.@entity[0]) >
    public ${entityName} findByLoginAndPassword(String user,String password){
        return getDao().findByLoginAndPassword(user, password);
    }
    </#if>
    
   <#if includeLoginSpring && (entityName = config.login.@entity[0]) >
    public ${entityName} findByLogin(String user){
        return getDao().findByLogin(user);
    }
    </#if>
}
</#list>